#include <torch/script.h> // 包含所有 Torch C++ API 的头文件
#include <iostream>
#include <vector>
#include <memory>
#include <cmath> // for sin and M_PI

int main(int argc, const char* argv[]) {
    if (argc != 2) {
        std::cerr << "用法: ./sinx_tester <path-to-scripted-model.pt>\n";
        return -1;
    }

    torch::jit::script::Module module;
    try {
        // 反序列化并加载模型
        module = torch::jit::load(argv[1]);
        std::cout << "模型加载成功！\n";
    }
    catch (const c10::Error& e) {
        std::cerr << "加载模型时出错:\n" << e.what();
        return -1;
    }

    // 准备输入数据
    // 和 Python 中的 torch.tensor([[M_PI/2], [M_PI/4]]) 类似
    std::vector<torch::jit::IValue> inputs;
    torch::Tensor input_tensor = torch::tensor({{M_PI / 2.0f}, {M_PI / 4.0f}, {M_PI / 1.0f}});
    inputs.push_back(input_tensor);

    // 使用模型进行推理
    // .toIValue() 将输出转换为一个通用的 IValue 类型
    at::Tensor output_tensor = module.forward(inputs).toTensor();

    // 打印结果
    auto output_accessor = output_tensor.accessor<float, 2>();
    auto input_accessor = input_tensor.accessor<float, 2>();

    for (int i = 0; i < output_accessor.size(0); ++i) {
        float input_val = input_accessor[i][0];
        float predicted_val = output_accessor[i][0];
        float actual_val = std::sin(input_val);
        
        std::cout.precision(4);
        std::cout << std::fixed;
        std::cout << "输入: " << input_val << " (" << (input_val/M_PI) << "*PI)\n";
        std::cout << "  -> 模型预测值: " << predicted_val << "\n";
        std::cout << "  -> 真实sin值: " << actual_val << "\n\n";
    }

    return 0;
}